'use strict'
/**
 * 
 */
angular.module('WDCC').service('sWSocket', ['$rootScope','$timeout','$q', function ($rootScope,$timeout,$q) {

	var socket;
	var sendFlag=0;
	
	
	this.openReader=function () {
		$rootScope.UserBean.patientIDInfo=null;
		//var host = "ws://192.168.1.211:8888";
		var host = "ws://127.0.0.1:8888";
		if(socket == null){
			socket = new WebSocket(host);
		}
		try {
			socket.onmessage = function (msg) {
				
				if (typeof msg.data == "string") {
					closeReader();
					$rootScope.UserBean.patientIDInfo=msg.data;
				}
				else{
					alert("连接异常,请检查是否成功安装华旭J15S驱动.");
				}
			};
			socket.onopen = function () {
				readIDCard(); 
				
			}
		}
		catch (ex) {
			alert("连接异常,请检查是否成功安装华旭J15S驱动.");
		}
	}	
	//读取身份信息
	function readIDCard() {
		try {
			socket.send("cvrscan");
		}
		catch (ex) {
			alert("请打开设备.");
		}
	}
	//关闭设备
	function closeReader() {
		try {
			socket.send("disconnet");
			socket.onclose = function () {
				closeSocket();
			}
			
		}
		catch (ex) {
			alert("请打开设备.");
		}
	}
	function closeSocket() {
		try {
			if(socket != null){
				socket.close();
				socket = null;
			}
		}
		catch (ex) {
		}
	};
	
}]);